package it.storm.solution;

/**
 * 1629. 按键持续时间最长的键
 * https://leetcode-cn.com/problems/slowest-key/
 */
public class Solutions_1629 {
    public static void main(String[] args) {
        int[] releaseTimes = {9, 29, 49, 50};
        String keysPressed = "cbcd";  // output: "c"

//        int[] releaseTimes = {12, 23, 36, 46, 62};
//        String keysPressed = "spuda";  // output: "a"

        char result = slowestKey(releaseTimes, keysPressed);
        System.out.println(result);
    }

    public static char slowestKey(int[] releaseTimes, String keysPressed) {
        char[] arr = keysPressed.toCharArray();
        // 初始值
        int maxTime = releaseTimes[0];
        char res = arr[0];

        for (int i = 1; i < releaseTimes.length; i++) {
            char c = arr[i];
            // 字符 c 的按键持续时间
            int curTime = releaseTimes[i] - releaseTimes[i - 1];
            if (curTime > maxTime) {
                maxTime = curTime;
                res = c;
            } else if (curTime == maxTime && c > res) {
                // 按键持续时间相同，且字符 c 大于字符 res
                res = c;
            }
        }
        return res;
    }
}
